/**
 * @file param_parser.c
 * @brief 参数解析工具实现
 * @details 本文件实现了JSON参数解析功能，用于解析入参JSON文件并生成
 *          结构化的参数传入API进行协议处理。
 * 
 * @author zyb
 * @date 2025-09-28
 * @version 1.0
 * 
 * @note 本模块是参数解析工具，用于处理JSON入参
 * @warning 参数解析结果直接影响协议处理，请确保参数正确性
 */

#include "param_parser.h"
#include "json_utils.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
// 定义模块名称
#define LOG_MODULE_NAME "PARAM_PARSER"

// 全局错误信息
static char g_error_message[256] = {0};

/* ==================== 函数实现 ==================== */

/**
 * @brief 初始化参数解析器
 * @details 初始化参数解析器模块
 * 
 * @return 初始化结果
 */
int param_parser_init(void) {
    LOG_DEBUG(LOG_MODULE_NAME, "初始化参数解析器模块");
    
    // 清空错误信息
    memset(g_error_message, 0, sizeof(g_error_message));
    
    LOG_INFO(LOG_MODULE_NAME, "参数解析器模块初始化完成");
    return 0;
}

/**
 * @brief 销毁参数解析器
 * @details 清理参数解析器资源
 */
void param_parser_destroy(void) {
    LOG_DEBUG(LOG_MODULE_NAME, "销毁参数解析器模块");
    
    // 清空错误信息
    memset(g_error_message, 0, sizeof(g_error_message));
    
    LOG_INFO(LOG_MODULE_NAME, "参数解析器模块销毁完成");
}

/**
 * @brief 解析遥测参数JSON文件
 * @details 从JSON文件解析遥测参数
 * 
 * @param json_file JSON文件路径
 * @param params 输出遥测参数结构
 * @return 解析结果
 */
param_result_t param_parser_parse_telemetry(const char *json_file, telemetry_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "解析遥测参数JSON文件: %s", json_file);
    
    if (!json_file || !params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 读取JSON文件
    FILE *file = fopen(json_file, "r");
    if (!file) {
        LOG_ERROR(LOG_MODULE_NAME, "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        snprintf(g_error_message, sizeof(g_error_message), "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        return PARAM_ERROR_FILE_NOT_FOUND;
    }
    
    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
    // 读取文件内容
    char *json_content = malloc(file_size + 1);
    if (!json_content) {
        LOG_ERROR(LOG_MODULE_NAME, "内存分配失败");
        snprintf(g_error_message, sizeof(g_error_message), "内存分配失败");
        fclose(file);
        return PARAM_ERROR_MEMORY;
    }
    
    size_t bytes_read = fread(json_content, 1, file_size, file);
    if (bytes_read != (size_t)file_size) {
        snprintf(g_error_message, sizeof(g_error_message), "文件读取不完整: 期望%ld字节，实际读取%zu字节", file_size, bytes_read);
        free(json_content);
        fclose(file);
        return PARAM_ERROR_FILE_READ;
    }
    json_content[file_size] = '\0';
    fclose(file);
    
    // 解析JSON
    void *json_root = json_utils_parse(json_content);
    free(json_content);
    
    if (!json_root) {
        LOG_ERROR(LOG_MODULE_NAME, "JSON解析失败: %s", json_utils_get_error());
        snprintf(g_error_message, sizeof(g_error_message), "JSON解析失败: %s", json_utils_get_error());
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析遥测数据
    void *telemetry_data_obj = json_utils_get_object(json_root, "telemetry_data");
    if (!telemetry_data_obj) {
        LOG_ERROR(LOG_MODULE_NAME, "未找到telemetry_data对象");
        snprintf(g_error_message, sizeof(g_error_message), "未找到telemetry_data对象");
        json_utils_delete(json_root);
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析设备ID和时间戳
    const char *device_id = json_utils_get_string(telemetry_data_obj, "device_id");
    if (device_id) {
        strncpy(params->telemetry_data.device_id, device_id, sizeof(params->telemetry_data.device_id) - 1);
        params->telemetry_data.device_id[sizeof(params->telemetry_data.device_id) - 1] = '\0';
    }
    
    // 直接生成当前时间戳，不依赖JSON中的timestamp字段
    params->telemetry_data.timestamp = time(NULL);
    
    // 解析测量值数组
    void *measurements_array = json_utils_get_array(telemetry_data_obj, "measurements");
    if (measurements_array) {
        int array_size = json_utils_get_array_size(measurements_array);
        params->telemetry_data.measurement_count = (array_size > 16) ? 16 : array_size;
        
        for (int i = 0; i < params->telemetry_data.measurement_count; i++) {
            void *measurement_obj = json_utils_get_array_item(measurements_array, i);
            if (measurement_obj) {
                const char *type = json_utils_get_string(measurement_obj, "type");
                if (type) {
                    strncpy(params->telemetry_data.measurements[i].type, type, sizeof(params->telemetry_data.measurements[i].type) - 1);
                    params->telemetry_data.measurements[i].type[sizeof(params->telemetry_data.measurements[i].type) - 1] = '\0';
                }
                
                double value;
                if (json_utils_get_number(measurement_obj, "value", &value) == JSON_RESULT_SUCCESS) {
                    params->telemetry_data.measurements[i].value = value;
                }
                
                const char *unit = json_utils_get_string(measurement_obj, "unit");
                if (unit) {
                    strncpy(params->telemetry_data.measurements[i].unit, unit, sizeof(params->telemetry_data.measurements[i].unit) - 1);
                    params->telemetry_data.measurements[i].unit[sizeof(params->telemetry_data.measurements[i].unit) - 1] = '\0';
                }
                
                const char *quality = json_utils_get_string(measurement_obj, "quality");
                if (quality) {
                    strncpy(params->telemetry_data.measurements[i].quality, quality, sizeof(params->telemetry_data.measurements[i].quality) - 1);
                    params->telemetry_data.measurements[i].quality[sizeof(params->telemetry_data.measurements[i].quality) - 1] = '\0';
                }
            }
        }
    }
    
    // 解析设备状态
    void *status_obj = json_utils_get_object(telemetry_data_obj, "status");
    if (status_obj) {
        bool online;
        if (json_utils_get_bool(status_obj, "online", &online) == JSON_RESULT_SUCCESS) {
            params->telemetry_data.status.online = online;
        }
        
        bool alarm;
        if (json_utils_get_bool(status_obj, "alarm", &alarm) == JSON_RESULT_SUCCESS) {
            params->telemetry_data.status.alarm = alarm;
        }
        
        bool maintenance_required;
        if (json_utils_get_bool(status_obj, "maintenance_required", &maintenance_required) == JSON_RESULT_SUCCESS) {
            params->telemetry_data.status.maintenance_required = maintenance_required;
        }
    }
    
    // 解析协议配置
    void *protocol_config_obj = json_utils_get_object(json_root, "protocol_config");
    if (protocol_config_obj) {
        double common_address;
        if (json_utils_get_number(protocol_config_obj, "common_address", &common_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.common_address = (uint16_t)common_address;
        }
        
        double information_object_address;
        if (json_utils_get_number(protocol_config_obj, "information_object_address", &information_object_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.information_object_address = (uint16_t)information_object_address;
        }
        
        double cause_of_transmission;
        if (json_utils_get_number(protocol_config_obj, "cause_of_transmission", &cause_of_transmission) == JSON_RESULT_SUCCESS) {
            params->protocol_config.cause_of_transmission = (uint8_t)cause_of_transmission;
        }
        
        double type_identification;
        if (json_utils_get_number(protocol_config_obj, "type_identification", &type_identification) == JSON_RESULT_SUCCESS) {
            params->protocol_config.type_identification = (uint8_t)type_identification;
        }
    }
    
    // 解析传输配置
    void *transmission_config_obj = json_utils_get_object(json_root, "transmission_config");
    if (transmission_config_obj) {
        const char *priority = json_utils_get_string(transmission_config_obj, "priority");
        if (priority) {
            strncpy(params->transmission_config.priority, priority, sizeof(params->transmission_config.priority) - 1);
            params->transmission_config.priority[sizeof(params->transmission_config.priority) - 1] = '\0';
        }
        
        double retry_count;
        if (json_utils_get_number(transmission_config_obj, "retry_count", &retry_count) == JSON_RESULT_SUCCESS) {
            params->transmission_config.retry_count = (int)retry_count;
        }
        
        double timeout_ms;
        if (json_utils_get_number(transmission_config_obj, "timeout_ms", &timeout_ms) == JSON_RESULT_SUCCESS) {
            params->transmission_config.timeout_ms = (uint32_t)timeout_ms;
        }
        
        bool encryption;
        if (json_utils_get_bool(transmission_config_obj, "encryption", &encryption) == JSON_RESULT_SUCCESS) {
            params->transmission_config.encryption = encryption;
        }
    }
    
    json_utils_delete(json_root);
    
    LOG_INFO(LOG_MODULE_NAME, "遥测参数解析成功: %s", json_file);
    return PARAM_SUCCESS;
}

/**
 * @brief 解析控制参数JSON文件
 * @details 从JSON文件解析控制参数
 * 
 * @param json_file JSON文件路径
 * @param params 输出控制参数结构
 * @return 解析结果
 */
param_result_t param_parser_parse_control(const char *json_file, control_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "解析控制参数JSON文件: %s", json_file);
    
    if (!json_file || !params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 读取JSON文件
    FILE *file = fopen(json_file, "r");
    if (!file) {
        LOG_ERROR(LOG_MODULE_NAME, "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        snprintf(g_error_message, sizeof(g_error_message), "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        return PARAM_ERROR_FILE_NOT_FOUND;
    }
    
    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
    // 读取文件内容
    char *json_content = malloc(file_size + 1);
    if (!json_content) {
        LOG_ERROR(LOG_MODULE_NAME, "内存分配失败");
        snprintf(g_error_message, sizeof(g_error_message), "内存分配失败");
        fclose(file);
        return PARAM_ERROR_MEMORY;
    }
    
    size_t bytes_read = fread(json_content, 1, file_size, file);
    if (bytes_read != (size_t)file_size) {
        snprintf(g_error_message, sizeof(g_error_message), "文件读取不完整: 期望%ld字节，实际读取%zu字节", file_size, bytes_read);
        free(json_content);
        fclose(file);
        return PARAM_ERROR_FILE_READ;
    }
    json_content[file_size] = '\0';
    fclose(file);
    
    // 解析JSON
    void *json_root = json_utils_parse(json_content);
    free(json_content);
    
    if (!json_root) {
        LOG_ERROR(LOG_MODULE_NAME, "JSON解析失败: %s", json_utils_get_error());
        snprintf(g_error_message, sizeof(g_error_message), "JSON解析失败: %s", json_utils_get_error());
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析控制命令 - 适配现有的JSON结构
    void *control_command_obj = json_utils_get_object(json_root, "control_data");
    if (!control_command_obj) {
        LOG_ERROR(LOG_MODULE_NAME, "未找到control_data对象");
        snprintf(g_error_message, sizeof(g_error_message), "未找到control_data对象");
        json_utils_delete(json_root);
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析命令基本信息 - 从根级别获取
    const char *command_id = json_utils_get_string(json_root, "signal_id");
    if (command_id) {
        strncpy(params->control_command.command_id, command_id, sizeof(params->control_command.command_id) - 1);
        params->control_command.command_id[sizeof(params->control_command.command_id) - 1] = '\0';
    }
    
    const char *device_id = json_utils_get_string(json_root, "device_id");
    if (device_id) {
        strncpy(params->control_command.device_id, device_id, sizeof(params->control_command.device_id) - 1);
        params->control_command.device_id[sizeof(params->control_command.device_id) - 1] = '\0';
    }
    
    const char *command_type = json_utils_get_string(json_root, "signal_type");
    if (command_type) {
        strncpy(params->control_command.command_type, command_type, sizeof(params->control_command.command_type) - 1);
        params->control_command.command_type[sizeof(params->control_command.command_type) - 1] = '\0';
    }
    
    // 直接生成当前时间戳，不依赖JSON中的timestamp字段
    params->control_command.timestamp = time(NULL);
    
    // 解析控制参数
    void *parameters_obj = json_utils_get_object(control_command_obj, "parameters");
    if (parameters_obj) {
        double target_voltage;
        if (json_utils_get_number(parameters_obj, "target_voltage", &target_voltage) == JSON_RESULT_SUCCESS) {
            params->control_command.parameters.target_voltage = target_voltage;
        }
        
        double target_current;
        if (json_utils_get_number(parameters_obj, "target_current", &target_current) == JSON_RESULT_SUCCESS) {
            params->control_command.parameters.target_current = target_current;
        }
        
        const char *operation_mode = json_utils_get_string(parameters_obj, "operation_mode");
        if (operation_mode) {
            strncpy(params->control_command.parameters.operation_mode, operation_mode, sizeof(params->control_command.parameters.operation_mode) - 1);
            params->control_command.parameters.operation_mode[sizeof(params->control_command.parameters.operation_mode) - 1] = '\0';
        }
        
        bool enable_protection;
        if (json_utils_get_bool(parameters_obj, "enable_protection", &enable_protection) == JSON_RESULT_SUCCESS) {
            params->control_command.parameters.enable_protection = enable_protection;
        }
    }
    
    // 解析执行配置
    void *execution_config_obj = json_utils_get_object(control_command_obj, "execution_config");
    if (execution_config_obj) {
        bool immediate;
        if (json_utils_get_bool(execution_config_obj, "immediate", &immediate) == JSON_RESULT_SUCCESS) {
            params->control_command.execution_config.immediate = immediate;
        }
        
        bool confirmation_required;
        if (json_utils_get_bool(execution_config_obj, "confirmation_required", &confirmation_required) == JSON_RESULT_SUCCESS) {
            params->control_command.execution_config.confirmation_required = confirmation_required;
        }
        
        bool rollback_on_failure;
        if (json_utils_get_bool(execution_config_obj, "rollback_on_failure", &rollback_on_failure) == JSON_RESULT_SUCCESS) {
            params->control_command.execution_config.rollback_on_failure = rollback_on_failure;
        }
        
        const char *scheduled_time_str = json_utils_get_string(execution_config_obj, "scheduled_time");
        if (scheduled_time_str) {
            params->control_command.execution_config.scheduled_time = time(NULL);
        }
    }
    
    // 解析协议配置
    void *protocol_config_obj = json_utils_get_object(json_root, "protocol_config");
    if (protocol_config_obj) {
        double common_address;
        if (json_utils_get_number(protocol_config_obj, "common_address", &common_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.common_address = (uint16_t)common_address;
        }
        
        double information_object_address;
        if (json_utils_get_number(protocol_config_obj, "information_object_address", &information_object_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.information_object_address = (uint16_t)information_object_address;
        }
        
        double cause_of_transmission;
        if (json_utils_get_number(protocol_config_obj, "cause_of_transmission", &cause_of_transmission) == JSON_RESULT_SUCCESS) {
            params->protocol_config.cause_of_transmission = (uint8_t)cause_of_transmission;
        }
        
        double type_identification;
        if (json_utils_get_number(protocol_config_obj, "type_identification", &type_identification) == JSON_RESULT_SUCCESS) {
            params->protocol_config.type_identification = (uint8_t)type_identification;
        }
    }
    
    // 解析安全配置
    void *safety_config_obj = json_utils_get_object(json_root, "safety_config");
    if (safety_config_obj) {
        double max_voltage;
        if (json_utils_get_number(safety_config_obj, "max_voltage", &max_voltage) == JSON_RESULT_SUCCESS) {
            params->safety_config.max_voltage = max_voltage;
        }
        
        double max_current;
        if (json_utils_get_number(safety_config_obj, "max_current", &max_current) == JSON_RESULT_SUCCESS) {
            params->safety_config.max_current = max_current;
        }
        
        bool emergency_stop;
        if (json_utils_get_bool(safety_config_obj, "emergency_stop", &emergency_stop) == JSON_RESULT_SUCCESS) {
            params->safety_config.emergency_stop = emergency_stop;
        }
        
        bool interlock_required;
        if (json_utils_get_bool(safety_config_obj, "interlock_required", &interlock_required) == JSON_RESULT_SUCCESS) {
            params->safety_config.interlock_required = interlock_required;
        }
    }
    
    json_utils_delete(json_root);
    
    LOG_INFO(LOG_MODULE_NAME, "控制参数解析成功: %s", json_file);
    return PARAM_SUCCESS;
}

/**
 * @brief 解析状态参数JSON文件
 * @details 从JSON文件解析状态参数
 * 
 * @param json_file JSON文件路径
 * @param params 输出状态参数结构
 * @return 解析结果
 */
param_result_t param_parser_parse_status(const char *json_file, status_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "解析状态参数JSON文件: %s", json_file);
    
    if (!json_file || !params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 读取JSON文件
    FILE *file = fopen(json_file, "r");
    if (!file) {
        LOG_ERROR(LOG_MODULE_NAME, "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        snprintf(g_error_message, sizeof(g_error_message), "无法打开JSON文件: %s, 错误: %s", json_file, strerror(errno));
        return PARAM_ERROR_FILE_NOT_FOUND;
    }
    
    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    
    // 读取文件内容
    char *json_content = malloc(file_size + 1);
    if (!json_content) {
        LOG_ERROR(LOG_MODULE_NAME, "内存分配失败");
        snprintf(g_error_message, sizeof(g_error_message), "内存分配失败");
        fclose(file);
        return PARAM_ERROR_MEMORY;
    }
    
    size_t bytes_read = fread(json_content, 1, file_size, file);
    if (bytes_read != (size_t)file_size) {
        snprintf(g_error_message, sizeof(g_error_message), "文件读取不完整: 期望%ld字节，实际读取%zu字节", file_size, bytes_read);
        free(json_content);
        fclose(file);
        return PARAM_ERROR_FILE_READ;
    }
    json_content[file_size] = '\0';
    fclose(file);
    
    // 解析JSON
    void *json_root = json_utils_parse(json_content);
    free(json_content);
    
    if (!json_root) {
        LOG_ERROR(LOG_MODULE_NAME, "JSON解析失败: %s", json_utils_get_error());
        snprintf(g_error_message, sizeof(g_error_message), "JSON解析失败: %s", json_utils_get_error());
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析状态查询
    void *status_query_obj = json_utils_get_object(json_root, "status_query");
    if (!status_query_obj) {
        LOG_ERROR(LOG_MODULE_NAME, "未找到status_query对象");
        snprintf(g_error_message, sizeof(g_error_message), "未找到status_query对象");
        json_utils_delete(json_root);
        return PARAM_ERROR_PARSE_FAILED;
    }
    
    // 解析查询基本信息
    const char *query_id = json_utils_get_string(status_query_obj, "query_id");
    if (query_id) {
        strncpy(params->status_query.query_id, query_id, sizeof(params->status_query.query_id) - 1);
        params->status_query.query_id[sizeof(params->status_query.query_id) - 1] = '\0';
    }
    
    const char *device_id = json_utils_get_string(status_query_obj, "device_id");
    if (device_id) {
        strncpy(params->status_query.device_id, device_id, sizeof(params->status_query.device_id) - 1);
        params->status_query.device_id[sizeof(params->status_query.device_id) - 1] = '\0';
    }
    
    const char *query_type = json_utils_get_string(status_query_obj, "query_type");
    if (query_type) {
        strncpy(params->status_query.query_type, query_type, sizeof(params->status_query.query_type) - 1);
        params->status_query.query_type[sizeof(params->status_query.query_type) - 1] = '\0';
    }
    
    // 直接生成当前时间戳，不依赖JSON中的timestamp字段
    params->status_query.timestamp = time(NULL);
    
    // 解析请求数据数组
    void *requested_data_array = json_utils_get_array(status_query_obj, "requested_data");
    if (requested_data_array) {
        int array_size = json_utils_get_array_size(requested_data_array);
        params->status_query.requested_data_count = (array_size > 5) ? 5 : array_size;
        
        for (int i = 0; i < params->status_query.requested_data_count; i++) {
            void *data_item = json_utils_get_array_item(requested_data_array, i);
            if (data_item && json_utils_is_type(data_item, "string")) {
                const char *data_str = json_utils_get_string(data_item, NULL);
                if (data_str) {
                    strncpy(params->status_query.requested_data[i], data_str, sizeof(params->status_query.requested_data[i]) - 1);
                    params->status_query.requested_data[i][sizeof(params->status_query.requested_data[i]) - 1] = '\0';
                }
            }
        }
    }
    
    // 解析响应配置
    void *response_config_obj = json_utils_get_object(status_query_obj, "response_config");
    if (response_config_obj) {
        const char *format = json_utils_get_string(response_config_obj, "format");
        if (format) {
            strncpy(params->status_query.response_config.format, format, sizeof(params->status_query.response_config.format) - 1);
            params->status_query.response_config.format[sizeof(params->status_query.response_config.format) - 1] = '\0';
        }
        
        bool include_timestamps;
        if (json_utils_get_bool(response_config_obj, "include_timestamps", &include_timestamps) == JSON_RESULT_SUCCESS) {
            params->status_query.response_config.include_timestamps = include_timestamps;
        }
        
        bool include_quality_flags;
        if (json_utils_get_bool(response_config_obj, "include_quality_flags", &include_quality_flags) == JSON_RESULT_SUCCESS) {
            params->status_query.response_config.include_quality_flags = include_quality_flags;
        }
        
        bool compression;
        if (json_utils_get_bool(response_config_obj, "compression", &compression) == JSON_RESULT_SUCCESS) {
            params->status_query.response_config.compression = compression;
        }
    }
    
    // 解析协议配置
    void *protocol_config_obj = json_utils_get_object(json_root, "protocol_config");
    if (protocol_config_obj) {
        double common_address;
        if (json_utils_get_number(protocol_config_obj, "common_address", &common_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.common_address = (uint16_t)common_address;
        }
        
        double information_object_address;
        if (json_utils_get_number(protocol_config_obj, "information_object_address", &information_object_address) == JSON_RESULT_SUCCESS) {
            params->protocol_config.information_object_address = (uint16_t)information_object_address;
        }
        
        double cause_of_transmission;
        if (json_utils_get_number(protocol_config_obj, "cause_of_transmission", &cause_of_transmission) == JSON_RESULT_SUCCESS) {
            params->protocol_config.cause_of_transmission = (uint8_t)cause_of_transmission;
        }
        
        double type_identification;
        if (json_utils_get_number(protocol_config_obj, "type_identification", &type_identification) == JSON_RESULT_SUCCESS) {
            params->protocol_config.type_identification = (uint8_t)type_identification;
        }
    }
    
    // 解析过滤配置
    void *filtering_config_obj = json_utils_get_object(json_root, "filtering_config");
    if (filtering_config_obj) {
        // 解析时间范围
        void *time_range_obj = json_utils_get_object(filtering_config_obj, "time_range");
        if (time_range_obj) {
            const char *start_time_str = json_utils_get_string(time_range_obj, "start_time");
            if (start_time_str) {
                params->filtering_config.time_range.start_time = time(NULL);
            }
            
            const char *end_time_str = json_utils_get_string(time_range_obj, "end_time");
            if (end_time_str) {
                params->filtering_config.time_range.end_time = time(NULL);
            }
        }
        
        // 解析数据类型数组
        void *data_types_array = json_utils_get_array(filtering_config_obj, "data_types");
        if (data_types_array) {
            int array_size = json_utils_get_array_size(data_types_array);
            params->filtering_config.data_type_count = (array_size > 3) ? 3 : array_size;
            
            for (int i = 0; i < params->filtering_config.data_type_count; i++) {
                void *data_type_item = json_utils_get_array_item(data_types_array, i);
                if (data_type_item && json_utils_is_type(data_type_item, "string")) {
                    const char *data_type_str = json_utils_get_string(data_type_item, NULL);
                    if (data_type_str) {
                        strncpy(params->filtering_config.data_types[i], data_type_str, sizeof(params->filtering_config.data_types[i]) - 1);
                        params->filtering_config.data_types[i][sizeof(params->filtering_config.data_types[i]) - 1] = '\0';
                    }
                }
            }
        }
        
        const char *quality_filter = json_utils_get_string(filtering_config_obj, "quality_filter");
        if (quality_filter) {
            strncpy(params->filtering_config.quality_filter, quality_filter, sizeof(params->filtering_config.quality_filter) - 1);
            params->filtering_config.quality_filter[sizeof(params->filtering_config.quality_filter) - 1] = '\0';
        }
    }
    
    json_utils_delete(json_root);
    
    LOG_INFO(LOG_MODULE_NAME, "状态参数解析成功: %s", json_file);
    return PARAM_SUCCESS;
}

/**
 * @brief 验证遥测参数
 * @details 验证遥测参数的有效性
 * 
 * @param params 遥测参数结构
 * @return 验证结果
 */
param_result_t param_parser_validate_telemetry(const telemetry_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "验证遥测参数");
    
    if (!params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 验证设备ID
    if (strlen(params->telemetry_data.device_id) == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "设备ID为空");
        snprintf(g_error_message, sizeof(g_error_message), "设备ID为空");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证测量值数量
    if (params->telemetry_data.measurement_count <= 0 || params->telemetry_data.measurement_count > 16) {
        LOG_ERROR(LOG_MODULE_NAME, "测量值数量无效: %d", params->telemetry_data.measurement_count);
        snprintf(g_error_message, sizeof(g_error_message), "测量值数量无效: %d", params->telemetry_data.measurement_count);
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证协议配置
    if (params->protocol_config.common_address == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "公共地址无效");
        snprintf(g_error_message, sizeof(g_error_message), "公共地址无效");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    LOG_DEBUG(LOG_MODULE_NAME, "遥测参数验证成功");
    return PARAM_SUCCESS;
}

/**
 * @brief 验证控制参数
 * @details 验证控制参数的有效性
 * 
 * @param params 控制参数结构
 * @return 验证结果
 */
param_result_t param_parser_validate_control(const control_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "验证控制参数");
    
    if (!params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 验证命令ID
    if (strlen(params->control_command.command_id) == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "命令ID为空");
        snprintf(g_error_message, sizeof(g_error_message), "命令ID为空");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证设备ID
    if (strlen(params->control_command.device_id) == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "设备ID为空");
        snprintf(g_error_message, sizeof(g_error_message), "设备ID为空");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证安全配置
    if (params->safety_config.max_voltage <= 0 || params->safety_config.max_current <= 0) {
        LOG_ERROR(LOG_MODULE_NAME, "安全配置无效");
        snprintf(g_error_message, sizeof(g_error_message), "安全配置无效");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    LOG_DEBUG(LOG_MODULE_NAME, "控制参数验证成功");
    return PARAM_SUCCESS;
}

/**
 * @brief 验证状态参数
 * @details 验证状态参数的有效性
 * 
 * @param params 状态参数结构
 * @return 验证结果
 */
param_result_t param_parser_validate_status(const status_params_t *params) {
    LOG_DEBUG(LOG_MODULE_NAME, "验证状态参数");
    
    if (!params) {
        LOG_ERROR(LOG_MODULE_NAME, "参数为空");
        snprintf(g_error_message, sizeof(g_error_message), "参数为空");
        return PARAM_ERROR_INVALID_PARAM;
    }
    
    // 验证查询ID
    if (strlen(params->status_query.query_id) == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "查询ID为空");
        snprintf(g_error_message, sizeof(g_error_message), "查询ID为空");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证设备ID
    if (strlen(params->status_query.device_id) == 0) {
        LOG_ERROR(LOG_MODULE_NAME, "设备ID为空");
        snprintf(g_error_message, sizeof(g_error_message), "设备ID为空");
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    // 验证请求数据数量
    if (params->status_query.requested_data_count <= 0 || params->status_query.requested_data_count > 5) {
        LOG_ERROR(LOG_MODULE_NAME, "请求数据数量无效: %d", params->status_query.requested_data_count);
        snprintf(g_error_message, sizeof(g_error_message), "请求数据数量无效: %d", params->status_query.requested_data_count);
        return PARAM_ERROR_VALIDATION_FAILED;
    }
    
    LOG_DEBUG(LOG_MODULE_NAME, "状态参数验证成功");
    return PARAM_SUCCESS;
}

/**
 * @brief 获取参数解析错误信息
 * @details 获取最后一次参数解析操作的错误信息
 * 
 * @return 错误信息字符串
 */
const char* param_parser_get_error(void) {
    LOG_DEBUG(LOG_MODULE_NAME, "获取参数解析错误信息: %s", g_error_message);
    return g_error_message;
}
